import java.util.*;


//比较器
class Student implements Comparable<Student>{
    public int age;
    public String name;

    //只是比较对象的大小关系，返回值是差值，正数，负数，0
    @Override
    public int compareTo(Student o) {
        return o.age-this.age;//大根堆
        //age - o.age --小根堆
    }



    //只是比较是否相等，返回值是boolean类型
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }


    @Override
    public int hashCode() {
        return Objects.hash(age, name);
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}


//年龄比较器
class AgeComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
        //o2.age - o1.age --大根堆
    }
}

class NameComparator implements Comparator<Student>{
    @Override
    //重写
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
        //name是string类型，引用变量conparable的方法
    }
}

class Imp implements Comparator<Integer>{

    @Override
    public int compare(Integer o1, Integer o2) {
        return o2-o1;
    }
}


public class Test {
    //找到前k个最小的数据
    public static int[] smallestk1(int []array,int k){
        PriorityQueue<Integer> minHeap =new PriorityQueue<>();
        for(int x:array){
            minHeap.offer(x);
        }
        //小根堆里面已经把数组所有的元素存储起来了 k*logn
        int[] ret = new int[k];
        for (int i = 0; i< k; i++){
            ret[i] = minHeap.poll();
        }
        return ret;
    }

    public static int[] smallestk(int []array,int k){
        int[] ret = new int[k];
        if (k==0){
            return ret;
        }
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(k,new Imp());
        for (int i = 0; i < k ; i++){
            maxHeap.offer(array[i]);
        }
        for (int i = k; i < array.length; i++){
            //1.获取堆顶元素的值
            int top = maxHeap.peek();
            if(top > array[i]){
                maxHeap.poll();
                maxHeap.offer(array[i]);
            }
        }

        for (int i = 0; i < k ; i++){
            ret[i] = maxHeap.poll();
        }
        return ret;
    }


    public static void main(String[] args) {

    }

    public static void main4(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student();
        s1.age = 10;
        s2.age = 20;
        s1.name = "lisi";
        s2.name = "zhangshan";
        AgeComparator ageComparator = new AgeComparator();
        PriorityQueue<Student> priorityQueue = new PriorityQueue<>(ageComparator);
        priorityQueue.offer(s1);
        priorityQueue.offer(s2);
        Student ret = priorityQueue.poll();
        System.out.println(ret);
    }

    public static void main3(String[] args) {
        PriorityQueue<Integer> priorityQueue1 = new PriorityQueue<>();
        priorityQueue1.offer(11);
        priorityQueue1.offer(2);
        priorityQueue1.offer(3);
    }


    public static void main1(String[] args) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();

        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(222);
        //默认分配了一个长度为11的queue数组
        Queue<Integer> priorityQueue1 = new PriorityQueue<>(list);
        priorityQueue1.offer(11);
        priorityQueue1.offer(2);
        priorityQueue1.offer(3);
        System.out.println(priorityQueue1.poll());
        System.out.println(priorityQueue1.poll());
        System.out.println(priorityQueue1.poll());

        //Queue<Student> priorityQueue2 = new PriorityQueue<>();
        /*priorityQueue2.offer(new Student());
        priorityQueue2.offer(new Student());*/
        /*priorityQueue2.offer(null);*/

    }

    /*public static void main2(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student();
        s1.age = 10;
        s2.age = 20;
        s1.name = "lisi";
        s2.name = "lisi";
        AgeComparator ageComparator = new AgeComparator();
        System.out.println(ageComparator.compare(s1, s2));
        NameComparator nameComparator = new NameComparator();
        System.out.println(nameComparator.compare(s1, s2));
        System.out.println(s1.equals(s2));
        *//*System.out.println(s1.compareTo(s2));*//*
    }*/
}
